package org.wheel.module.core.datetime.serializer;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.wheel.module.core.datetime.TimeSerializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * description: LocalDateTimeDeserializer <br>
 * date: 2022/9/16 2:31 下午 <br>
 * author: hixx <br>
 * version: 1.0 <br>
 */
public final class LocalDateTimeToTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeSerializer.PATTERN_TIME);

    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String valueAsString = jsonParser.getValueAsString();
        if(StrUtil.isNotBlank(valueAsString)) {
            LocalDateTime parse = LocalDateTime.parse(valueAsString, dateTimeFormatter);
            return parse;
        }
        return null;
    }

}